package com.project.lk.nurseapp.common.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.project.lk.nurseapp.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 功能介绍：小条目的适配器
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class AllTagItemAdapter extends BaseRecycleViewAdapter {

    public List<String> mList;
    Context mContext;
    private int mSelectedItem = -1;

    public interface OnItemClickListener {
        void setOnItemClickListener(int position, View view);
    }


    private OnItemClickListener mListener;

    public void setItemClick(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public AllTagItemAdapter(List<String> list, Context context) {

        this.mList = list;
        this.mContext = context;
        notifyDataSetChanged();
    }

    public List<String> getmList() {

        return mList;
    }
//实现单选方法二： 设置数据集时，找到默认选中的pos


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.tag_item, parent, false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        ViewHolder viewHolder = (ViewHolder) holder;
        // 0,1,2,3,4,5   0,1,2,3  0,1,2  0,1
        viewHolder.tvTag.setText(String.valueOf(mList.get(position)));

//实现单选方法二： notifyItemChanged() 定向刷新两个视图
        //如果勾选的不是已经勾选状态的Item
        viewHolder.tvTag.setSelected(position == mSelectedItem);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mListener.setOnItemClickListener(position, view);
                mSelectedItem = position;
                notifyItemRangeChanged(0, mList.size());


            }
        });
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() : 0;
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_tag)
        TextView tvTag;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
